package statis

import (
	"fmt"
	"strconv"

	"qkstatis/db"

	rpc "qkstatis/rpc/client/qkleague"

	"doubimeizhi.com/datatable"
	"doubimeizhi.com/utility"

	log "github.com/cihub/seelog"
)

var washRoomProfitStatisInstance *washRoomProfitStatisService

type washRoomProfitStatisService struct{}

func GetWashRoomProfitStatisService() *washRoomProfitStatisService {
	if washRoomProfitStatisInstance == nil {
		washRoomProfitStatisInstance = &washRoomProfitStatisService{}
	}
	return washRoomProfitStatisInstance
}

//查询列表
func (p *washRoomProfitStatisService) ListWashRoomProfit(sessionId string, ltype int, conditions map[string]interface{}, needTotalRecords bool) ([]*WashRoomProfit, int) {
	dtq := p.washRoomProfitDtq(ltype)
	//dtq.SetPage(start, length)
	for key, val := range conditions {
		dtq.PushCondition(key, val)
	}
	sql, values := dtq.GenerateSQL()
	data := make([]*WashRoomProfit, 0)
	mysqlHelper := db.GetMySQLHelper()
	if results, err := mysqlHelper.Query(sql, values...); err == nil {
		if len(results) == 0 {
			log.Warnf("查询没有统计数据，conditions:%v", conditions)
			return make([]*WashRoomProfit, 0, 0), 0
		}
		//log.Infof("results :%s",results)
		for _, dt := range results {
			data = append(data, p.washRoomProfitParseRecords(dt, mysqlHelper, sessionId))
		}
	}
	recordsTotal := 0
	if needTotalRecords {
		s, v := dtq.GenerateTotalRecordSQL("total_records")
		if results, err := mysqlHelper.Query(s, v...); err == nil {
			if len(results) > 0 {
				recordsTotal = mysqlHelper.GetColInt(results[0], "total_records")
			}
		} else {
			log.Errorf("mysql err sql:%s, values:%v", err.Error(), v)
		}
	}
	//log.Infof("data : %s",data)
	return data, recordsTotal
}

func (p *washRoomProfitStatisService) washRoomProfitDtq(ltype int) *datatable.DataTableQuery {
	dtq := datatable.NewDataTableQuery()
	dtq.PushField(&datatable.FiledItem{Alias: "date_flag", Field: "date_flag", OrderBy: datatable.DESC})
	dtq.PushField(&datatable.FiledItem{Alias: "league_id", Field: "league_id"})
	dtq.PushField(&datatable.FiledItem{Alias: "wash_room_id", Field: "wash_room_id"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_order", Field: "washing_order"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_profit", Field: "washing_profit"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_total", Field: "washing_total"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_peer", Field: "washing_peer"})
	dtq.PushField(&datatable.FiledItem{Alias: "washer_num_active", Field: "washer_num_active"})
	dtq.PushField(&datatable.FiledItem{Alias: "washer_num_use", Field: "washer_num_use"})
	dtq.PushField(&datatable.FiledItem{Alias: "s_o1m", Field: "s_o1m"})
	dtq.PushField(&datatable.FiledItem{Alias: "s_p1m", Field: "s_p1m"})
	dtq.PushField(&datatable.FiledItem{Alias: "l_o1m", Field: "l_o1m"})
	dtq.PushField(&datatable.FiledItem{Alias: "l_p1m", Field: "l_p1m"})
	dtq.PushField(&datatable.FiledItem{Alias: "o1u", Field: "o1u"})
	dtq.PushField(&datatable.FiledItem{Alias: "u1m", Field: "u1m"})

	switch ltype {
	case 1:
		dtq.SetTableName("statis_washroom_profit_day")
	case 2:
		dtq.SetTableName("statis_washroom_profit_week")
	case 3:
		dtq.SetTableName("statis_washroom_profit_month")
	default:
		log.Errorf("washRoomProfitDtq, 未知参数ltype； ltype:%d", ltype)
		dtq.SetTableName("none")
	}
	return dtq
}

func (p *washRoomProfitStatisService) washRoomProfitParseRecords(dt map[string]interface{}, mysqlHelper *utility.MySQLHelper, sessionId string) *WashRoomProfit {
	wrp := &WashRoomProfit{}
	wrp.DateFlag = mysqlHelper.GetColString(dt, "date_flag")
	if len(wrp.DateFlag) == 6 {
		wrp.DateFlag += GetStatisService().date2week(wrp.DateFlag)
	}
	wrp.LeagueId = mysqlHelper.GetColString(dt, "league_id")
	wrp.WashRoomId = mysqlHelper.GetColString(dt, "wash_room_id")
	wrp.WashingOrder = mysqlHelper.GetColInt(dt, "washing_order")
	wrp.WashingPeer = mysqlHelper.GetColInt(dt, "washing_peer")
	wrp.WasherNumActive = mysqlHelper.GetColInt(dt, "washer_num_active")
	wrp.WasherNumUse = mysqlHelper.GetColInt(dt, "washer_num_use")
	wrp.So1m = mysqlHelper.GetColString(dt, "s_o1m")
	wrp.Lo1m = mysqlHelper.GetColString(dt, "l_o1m")
	wrp.O1u = mysqlHelper.GetColString(dt, "o1u")
	wrp.U1m = mysqlHelper.GetColString(dt, "u1m")
	tp := mysqlHelper.GetColInt(dt, "washing_total")
	wrp.WashingTotal =fmt.Sprintf("%.3f", float64(tp))
	wp := mysqlHelper.GetColInt(dt, "washing_profit")
	wrp.WashingProfit = fmt.Sprintf("%.3f", float64(wp)/float64(100))

	sp1m := mysqlHelper.GetColString(dt, "s_p1m")
	tmp, _ := strconv.ParseFloat(sp1m, 32)
	wrp.Sp1m = fmt.Sprintf("%.3f", tmp/float64(100))

	lp1m := mysqlHelper.GetColString(dt, "l_p1m")
	tmp, _ = strconv.ParseFloat(lp1m, 32)
	wrp.Lp1m = fmt.Sprintf("%.3f", tmp/float64(100))

	if ok, info := rpc.GetWashRoomInfo(wrp.WashRoomId, sessionId); ok {
		if info["league_name"].(string) == "" {
			wrp.LeagueName = info["mobile"].(string)
		} else {
			wrp.LeagueName = info["league_name"].(string)
		}

		if wrp.LeagueName == "" {
			log.Warnf("washroom-profit, league_name is empty; washroom_id:%s", wrp.WashRoomId)
		}

		/*	if info["washroom_name"] == "" {
				wrp.WashRoomName = "未知洗衣点"
			} else {
				wrp.WashRoomName = info["washroom_name"].(string)
			}*/
		log.Debug(info)
	}
	return wrp
}
